package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.pro.Workflow;
import com.qidianit.mes.pro.mapper.ProcessMapper;
import com.qidianit.mes.pro.mapper.WorkflowMapper;
import com.qidianit.mes.pro.service.WorkflowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: YKYB
 * @Date: 2025/9/18 18:53
 * @Description:
 */

@Service
public class WorkflowServiceImpl implements WorkflowService {

    @Autowired
    private WorkflowMapper workflowMapper;

    @Autowired
    private ProcessMapper processMapper;

    @Override
    public Result<List<Workflow>> selectByMaterialCode(String code) {
        List<Workflow> workflows = workflowMapper.selectListByMaterialCode(code);
        workflows.forEach(workflow -> {
            workflow.setProcesses(processMapper.selectProcessByWorkflowId(workflow.getId()));
        });
        return Result.success(workflows);
    }

    @Override
    public Result<PageInfo<Workflow>> selectList(Workflow workflow, Integer page, Integer size) {
        //开启分页
        PageHelper.startPage(page, size);
        List<Workflow> workflows = workflowMapper.selectList(workflow);
        return Result.success(new PageInfo<>(workflows));
    }

    @Override
    public Result<Workflow> selectByCode(String code) {
        return Result.success(workflowMapper.selectByCode(code));
    }

    @Override
    public Result<Workflow> saveWorkflow(Workflow workFlow) {
        return workflowMapper.addWorkFlow(workFlow) == 1 ?
                Result.success(workFlow) : Result.error("保存失败");
    }

    @Override
    public Result updateById(Workflow workFlow) {
        return workflowMapper.updateById(workFlow) == 1 ?
                Result.success() : Result.error("编辑失败");
    }

    @Override
    public Result deleteById(Long id) {
        return workflowMapper.deleteById(id) == 1 ?
                Result.success() : Result.error("删除失败");
    }

    @Override
    public Result batchDeleted(Long[] ids) {
        return workflowMapper.batchDelete(ids) == 1 ?
                Result.success() : Result.error("删除失败");
    }


}
